package cn.edu.buaa.purain.db;

import java.sql.Connection;

import javax.naming.InitialContext;
import javax.sql.DataSource;

public class ConnectionByJNDI implements IConnectionProvider {

	private DataSource ds;

	public DataSource getDataSource(String dbName) throws Exception{
		if(ds == null){
			InitialContext context = new InitialContext();
			ds = (DataSource)context.lookup("java:comp/env/jdbc/" + dbName);
		}
		
		return ds;
	}

	@Override
	public Connection getConnection(String dbName) throws Exception {
		return getDataSource(dbName).getConnection();
	}
}
